package com.zly.bscloud.service.impl;

import com.zly.bscloud.dto.ArticleDTO;
import com.zly.bscloud.dto.PreferencesDTO;
import com.zly.bscloud.entity.Article;
import com.zly.bscloud.entity.Preferences;
import com.zly.bscloud.entity.User;
import com.zly.bscloud.mapper.ArticleMapper;
import com.zly.bscloud.mapper.PreferencesMapper;
import com.zly.bscloud.mapper.UserMapper;
import com.zly.bscloud.service.PreferencesService;
import com.zly.bscloud.utils.entity.StatusCode;
import com.zly.bscloud.utils.exception.BpException;
import com.zly.bscloud.utils.recommendation.ItemCFDemo;
import com.zly.bscloud.utils.utils.BeanHelper;
import com.zly.bscloud.utils.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @Author zhouxiaoyu
 * @Date 2020/5/5 11:30
 * @Version 1.0
 */
@Service
public class PreferencesServiceImpl implements PreferencesService {
    @Autowired
    private PreferencesMapper preferencesMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    @Transactional
    public PreferencesDTO saveLikeByUId(Map<String , String> map){
        Preferences preferences = new Preferences();
        preferences.setId(UUIDUtils.UUID());
        preferences.setUser_id(map.get("user_id"));
        preferences.setArticle_id(map.get("article_id"));

        Article articleOne = new Article();
        articleOne.setId(map.get("user_id"));
        Article article = articleMapper.selectByPrimaryKey(map.get("article_id"));
        article.setIslikes(article.getIslikes()+1);
        articleMapper.updateByPrimaryKey(article);
        // 保存进数据库
        int count = preferencesMapper.insert(preferences);
        if (count != 1)
            throw new BpException(StatusCode.Failure);
        PreferencesDTO preferencesDTO = BeanHelper.copyProperties(preferences, PreferencesDTO.class);
        return preferencesDTO;
    }

    @Override
    @Transactional
    public Boolean getLike(String user_id,String article_id){
        Example example = new Example(Preferences.class);
        // 创建Criteria
        Example.Criteria criteria = example.createCriteria();
        // 添加条件
        criteria.andEqualTo("user_id", user_id);
        criteria.andEqualTo("article_id", article_id);
        //System.out.println(example);
        List<Preferences> preferencesList = preferencesMapper.selectByExample(example);
        if(preferencesList.isEmpty()){
            return false;
        }
        return true;
    }

    @Override
    @Transactional
    public List<ArticleDTO> getRecommendationList(String user_id){
        List<User> usersList = userMapper.selectAll();
        List<Article> articleList = articleMapper.selectAll();
        List<Preferences> preferencesList = preferencesMapper.selectAll();
        List<Article> recommendationList = ItemCFDemo.handle(user_id,usersList,articleList,preferencesList);
        List<ArticleDTO> articleDTOList = BeanHelper.copyWithCollection(recommendationList, ArticleDTO.class);
        return articleDTOList;

    }

}
